package org.csu.molediary.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.molediary.api.common.CommonResponse;
import org.csu.molediary.api.entity.Account;
import org.csu.molediary.api.entity.Friend;
import org.csu.molediary.api.persistence.AccountMapper;
import org.csu.molediary.api.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;


@Service("AccountService")
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountMapper accountMapper;

    //插入一个新的用户
    @Override
    public CommonResponse<Account> insertNewAccount(Account account) {

        int month = account.getMonth();
        int day = Integer.parseInt(account.getDate());
        String star = null;
        //通过出生日期计算星座
        switch (month) {
            case 1:
                star = day < 21 ? "摩羯座" : "水瓶座";
                break;
            case 2:
                star = day < 20 ? "水瓶座" : "双鱼座";
                break;
            case 3:
                star = day < 21 ? "双鱼座" : "白羊座";
                break;
            case 4:
                star = day < 21 ? "白羊座" : "金牛座";
                break;
            case 5:
                star = day < 22 ? "金牛座" : "双子座";
                break;
            case 6:
                star = day < 22 ? "双子座" : "巨蟹座";
                break;
            case 7:
                star = day < 23 ? "巨蟹座" : "狮子座";
                break;
            case 8:
                star = day < 24 ? "狮子座" : "处女座";
                break;
            case 9:
                star = day < 24 ? "处女座" : "天秤座";
                break;
            case 10:
                star = day < 24 ? "天秤座" : "天蝎座";
                break;
            case 11:
                star = day < 23 ? "天蝎座" : "射手座";
                break;
            case 12:
                star = day < 22 ? "射手座" : "摩羯座";
                break;

        }
        account.setStar(star);

        //插入整个用户信息
        accountMapper.insert(account);
        return CommonResponse.createForSuccess(account);
    }

    //通过id查询某个account信息
    @Override
    public CommonResponse<Account> selectAccountById(String userid) {
        QueryWrapper<Account> accountQueryWrapper=new QueryWrapper<>();
        accountQueryWrapper.eq("id",userid);
        Account account=accountMapper.selectOne(accountQueryWrapper);
        System.out.print(account);
        if(account==null){
            return CommonResponse.createForSuccessMessage("没有该用户");
        }
        return CommonResponse.createForSuccess(account);
    }

    @Override
    public CommonResponse<Account> updateAccountInformation(Account account) {

        int month = account.getMonth();
        int day = Integer.parseInt(account.getDate());
        String star = null;
        //通过出生日期计算星座
        switch (month) {
            case 1:
                star = day < 21 ? "摩羯座" : "水瓶座";
                break;
            case 2:
                star = day < 20 ? "水瓶座" : "双鱼座";
                break;
            case 3:
                star = day < 21 ? "双鱼座" : "白羊座";
                break;
            case 4:
                star = day < 21 ? "白羊座" : "金牛座";
                break;
            case 5:
                star = day < 22 ? "金牛座" : "双子座";
                break;
            case 6:
                star = day < 22 ? "双子座" : "巨蟹座";
                break;
            case 7:
                star = day < 23 ? "巨蟹座" : "狮子座";
                break;
            case 8:
                star = day < 24 ? "狮子座" : "处女座";
                break;
            case 9:
                star = day < 24 ? "处女座" : "天秤座";
                break;
            case 10:
                star = day < 24 ? "天秤座" : "天蝎座";
                break;
            case 11:
                star = day < 23 ? "天蝎座" : "射手座";
                break;
            case 12:
                star = day < 22 ? "射手座" : "摩羯座";
                break;

        }
        account.setStar(star);

        UpdateWrapper<Account> accountUpdateWrapper=new UpdateWrapper<>();
        accountUpdateWrapper.eq("id",account.getId());
        accountMapper.update(account,accountUpdateWrapper);

        return CommonResponse.createForSuccess(account);
    }

    @Override
    //通过name模糊查询用户
    public CommonResponse<List<Account>> getAccountByNameLike(String namelike) {
        QueryWrapper<Account> accountQueryWrapper = new QueryWrapper<>();
        accountQueryWrapper.like("name",namelike).or().like("id",namelike);
        List<Account> accountList = accountMapper.selectList(accountQueryWrapper);

        if(accountList.isEmpty()){
            return CommonResponse.createForError("没有查询到昵称中含有该字段的用户");
        }
        return CommonResponse.createForSuccess("查询好友成功",accountList);
    }
}
